package com.auemall.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.auemall.constant.ApplicationConstant;
import com.auemall.dao.TAttributePOMapper;
import com.auemall.dao.TAttributeValuesPOMapper;
import com.auemall.dao.TProductAttributePOMapper;
import com.auemall.po.TAttributePO;
import com.auemall.po.TAttributePOExample;
import com.auemall.po.TAttributeValuesPO;
import com.auemall.po.TAttributeValuesPOExample;
import com.auemall.po.TProductAttributePO;
import com.auemall.po.TProductAttributePOExample;
import com.auemall.service.ItemParamService;
import com.auemall.utils.EUDataGridResult;
import com.auemall.utils.StringUtils;
import com.auemall.vo.AttributeRespVO;
import com.auemall.vo.AttributeVO;
import com.auemall.vo.AttributeValueVO;
import com.auemall.vo.BaseRespVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class ItemParamServiceImpl implements ItemParamService {
    
    private Logger logger = Logger.getLogger(ItemParamServiceImpl.class);

	@Autowired
	private TProductAttributePOMapper productAttributePOMapper;
	@Autowired
	private TAttributePOMapper attributePOMapper;
	@Autowired
	private TAttributeValuesPOMapper attributeValuesPOMapper;
	
    @Override
    public EUDataGridResult selectAttributeList(AttributeVO req,int page, int rows)
            throws Exception {
        EUDataGridResult respVO = new EUDataGridResult();
        List<AttributeRespVO> respList = new ArrayList<AttributeRespVO>();
        if(req == null){
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("请求参数为空！");
            logger.error(respVO.getMsg());
            return respVO;
        }
        TAttributePOExample example = new TAttributePOExample();
        TAttributePOExample.Criteria criteria = example.createCriteria();
        if(null != req.getCategoryId()){
            criteria.andCategoryIdEqualTo(req.getCategoryId());
        }
        if(null != req.getUsageMode()){
            criteria.andUsageModeEqualTo(req.getUsageMode());
        }
        PageHelper.startPage(page, rows);
        List<TAttributePO> list = attributePOMapper.selectByExample(example);
        if(list != null && list.size() > 0){
            for(TAttributePO po : list){
                AttributeRespVO vo = new AttributeRespVO();
                vo.setAttributeName(po.getAttributeName());
                if(po.getId() != null){
                    vo.setId(po.getId().toString());
                    //获取属性值列表
                    List<AttributeValueVO> attributeValuesList = new ArrayList<AttributeValueVO>();
                    TAttributeValuesPOExample example2 = new TAttributeValuesPOExample();
                    TAttributeValuesPOExample.Criteria criteria2 = example2.createCriteria();
                    criteria2.andAttributeIdEqualTo(po.getId());
                    List<TAttributeValuesPO> attributeValues = attributeValuesPOMapper.selectByExample(example2);
                    if(attributeValues != null && attributeValues.size() > 0){
                        for(TAttributeValuesPO attributeValuesPO : attributeValues){
                            AttributeValueVO attributeValueVO = new AttributeValueVO();
                            attributeValueVO.setAttributeId(attributeValuesPO.getAttributeId().toString());
                            attributeValueVO.setValueStr(attributeValuesPO.getValueStr());
                            attributeValueVO.setId(attributeValuesPO.getId().toString());
                            attributeValuesList.add(attributeValueVO);
                        }
                        vo.setAttributeValues(attributeValuesList);
                    }
                }
                if(po.getCategoryId() != null){
                    vo.setCategoryId(po.getCategoryId().toString());
                }
                if(po.getUsageMode() != null){
                    vo.setUsageMode(po.getUsageMode().toString());
                }
                respList.add(vo);
            }
        }
        EUDataGridResult result = new EUDataGridResult();
        result.setRows(respList);
        //取记录总条数
        PageInfo<TAttributePO> pageInfo = new PageInfo<>(list);
        result.setTotal(pageInfo.getTotal());
        result.setMsg("获取属性列表成功");
        result.setResult(ApplicationConstant.RESULT_SUCCESS);
        return result;
    }

    @Override
    @Transactional
    public BaseRespVO insertAttribute(AttributeVO req) throws Exception {
        BaseRespVO respVO = new BaseRespVO();
        if(req == null){
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("请求参数为空！");
            logger.error(respVO.getMsg());
            return respVO;
        }
        TAttributePO record = new TAttributePO();
        BeanUtils.copyProperties(req, record);
        attributePOMapper.insert(record);
        if(StringUtils.isNotBlank(req.getValueStr())){
            TAttributeValuesPO record2 = new TAttributeValuesPO();
            record2.setAttributeId(record.getId());
            record2.setValueStr(req.getValueStr());
            attributeValuesPOMapper.insert(record2);
        }
        respVO.setResult(ApplicationConstant.RESULT_SUCCESS);
        respVO.setMsg("增加商品信息成功");
        return respVO;
    }

    @Override
    @Transactional
    public BaseRespVO updateAttribute(AttributeVO req) throws Exception {
        BaseRespVO respVO = new BaseRespVO();
        if(req == null){
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("请求参数为空！");
            logger.error(respVO.getMsg());
            return respVO;
        }
        if(req.getId() == null){
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("请求参数[ID]为空！");
            logger.error(respVO.getMsg());
            return respVO;
        }
        TAttributePO record = new TAttributePO();
        BeanUtils.copyProperties(req, record);
        attributePOMapper.updateByPrimaryKeySelective(record);
        respVO.setResult(ApplicationConstant.RESULT_SUCCESS);
        respVO.setMsg("修改商品信息成功");
        return respVO;
    }

    @Override
    @Transactional
    public BaseRespVO deleteAttribute(String id) throws Exception {
        BaseRespVO respVO = new BaseRespVO();
        if(StringUtils.isBlank(id)){
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("请求参数[id]为空！");
            logger.error(respVO.getMsg());
            return respVO;
        }
        attributePOMapper.deleteByPrimaryKey(Integer.parseInt(id));
        TAttributeValuesPOExample example = new TAttributeValuesPOExample();
        TAttributeValuesPOExample.Criteria criteria = example.createCriteria();
        criteria.andAttributeIdEqualTo(Integer.parseInt(id));
        List<TAttributeValuesPO> list = attributeValuesPOMapper.selectByExample(example);
        if(list != null && list.size() > 0){
            attributeValuesPOMapper.deleteByExample(example);
        }
        respVO.setResult(ApplicationConstant.RESULT_SUCCESS);
        respVO.setMsg("删除商品信息成功");
        return respVO;
    }

    @Override
    @Transactional
    public BaseRespVO insertAttributeValue(AttributeVO req) throws Exception {
        BaseRespVO respVO = new BaseRespVO();
        if(req == null){
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("请求参数为空！");
            logger.error(respVO.getMsg());
            return respVO;
        }
        if(req.getId() == null){
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("请求参数[ID]为空！");
            logger.error(respVO.getMsg());
            return respVO;
        }
        if(StringUtils.isBlank(req.getValueStr())){
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("请求参数[valueStr 属性值]为空！");
            logger.error(respVO.getMsg());
            return respVO;
        }
        TAttributeValuesPO record2 = new TAttributeValuesPO();
        record2.setAttributeId(req.getId());
        record2.setValueStr(req.getValueStr());
        attributeValuesPOMapper.insert(record2);
        respVO.setResult(ApplicationConstant.RESULT_SUCCESS);
        respVO.setMsg("增加商品属性值成功");
        return respVO;
    }

    @Override
    @Transactional
    public BaseRespVO deleteAttributeValue(String id) throws Exception {
        BaseRespVO respVO = new BaseRespVO();
        if(StringUtils.isBlank(id)){
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("请求参数[id]为空！");
            logger.error(respVO.getMsg());
            return respVO;
        }
        attributeValuesPOMapper.deleteByPrimaryKey(Integer.parseInt(id));
        respVO.setResult(ApplicationConstant.RESULT_SUCCESS);
        respVO.setMsg("删除商品属性值成功");
        return respVO;
    }

    @Override
    public EUDataGridResult selectAttrByProductId(AttributeVO req,int page, int rows) throws Exception {
        EUDataGridResult respVO = new EUDataGridResult();
        List<AttributeRespVO> respList = new ArrayList<AttributeRespVO>();
        if(req == null){
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("请求参数为空！");
            logger.error(respVO.getMsg());
            return respVO;
        }
       
        TAttributePOExample example = new TAttributePOExample();
        TAttributePOExample.Criteria criteria = example.createCriteria();
        if(null != req.getCategoryId()){
            criteria.andCategoryIdEqualTo(req.getCategoryId());
        }
        if(null != req.getUsageMode()){
            criteria.andUsageModeEqualTo(req.getUsageMode());
        }
        PageHelper.startPage(page, rows);
        List<TAttributePO> list = attributePOMapper.selectByExample(example);
        if(list != null && list.size() > 0){
            for(TAttributePO po : list){
                AttributeRespVO vo = new AttributeRespVO();
                vo.setAttributeName(po.getAttributeName());
                if(po.getId() != null){
                    vo.setId(po.getId().toString());
                    //获取属性值列表
                    List<AttributeValueVO> attributeValuesList = new ArrayList<AttributeValueVO>();
                    TAttributeValuesPOExample example2 = new TAttributeValuesPOExample();
                    TAttributeValuesPOExample.Criteria criteria2 = example2.createCriteria();
                    criteria2.andAttributeIdEqualTo(po.getId());
                    List<TAttributeValuesPO> attributeValues = attributeValuesPOMapper.selectByExample(example2);
                    if(attributeValues != null && attributeValues.size() > 0){
                        for(TAttributeValuesPO attributeValuesPO : attributeValues){
                            AttributeValueVO attributeValueVO = new AttributeValueVO();
                            attributeValueVO.setAttributeId(attributeValuesPO.getAttributeId().toString());
                            attributeValueVO.setValueStr(attributeValuesPO.getValueStr());
                            attributeValueVO.setId(attributeValuesPO.getId().toString());
                            //判断是否选中
                            //判断商品id不为空，查询与商品相关属性list
                            if(StringUtils.isNotBlank(req.getProductId())){
                                TProductAttributePOExample example3 = new TProductAttributePOExample();
                                TProductAttributePOExample.Criteria criteria3 = example3.createCriteria();
                                criteria3.andProductIdEqualTo(Integer.parseInt(req.getProductId()));
                                List<TProductAttributePO> list3 = productAttributePOMapper.selectByExample(example3);
                                if(list3 != null && list3.size() > 0){
                                    for(TProductAttributePO po3 : list3){
                                        if(po3.getValueId().equals(attributeValuesPO.getId())){
                                            attributeValueVO.setChecked("1");  //checked =1，需要选中
                                        }
                                    }
                                }
                            }else{
                                attributeValueVO.setChecked("0");  //checked =0，不需要选中操作，没有商品ID
                            }
                            
                            attributeValuesList.add(attributeValueVO);
                        }
                        vo.setAttributeValues(attributeValuesList);
                    }
                }
                if(po.getCategoryId() != null){
                    vo.setCategoryId(po.getCategoryId().toString());
                }
                if(po.getUsageMode() != null){
                    vo.setUsageMode(po.getUsageMode().toString());
                }
                respList.add(vo);
            }
        }
        EUDataGridResult result = new EUDataGridResult();
        result.setRows(respList);
        //取记录总条数
        PageInfo<TAttributePO> pageInfo = new PageInfo<>(list);
        result.setTotal(pageInfo.getTotal());
        result.setMsg("获取属性列表成功");
        result.setResult(ApplicationConstant.RESULT_SUCCESS);
        return result;
    }
	

}
